package com.suredy.app.porject.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.app.porject.model.Biddinger;
import com.suredy.core.service.BaseSrvWithEntity;

@Service
public class BiddingerSrv extends BaseSrvWithEntity<Biddinger> {

	public BiddingerSrv() {
		this.addAnDefOrder("owner", true);
		this.addAnDefOrder("company", true);
	}

	@Override
	public DetachedCriteria getDc(Biddinger t) {
		DetachedCriteria dc = super.getDc(t);

		if (t == null)
			return dc;

		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		if (!StringUtils.isBlank(t.getCompany())) {
			dc.add(Restrictions.like("company", t.getCompany(), MatchMode.ANYWHERE));
		}
		if (!StringUtils.isBlank(t.getAddr())) {
			dc.add(Restrictions.like("addr", t.getAddr(), MatchMode.ANYWHERE));
		}
		if (t.getOwner() != null) {
			dc.add(Restrictions.eq("owner", t.getOwner()));
		}

		return dc;
	}

	public int deleteByIds(String[] ids) {
		if (ids == null || ids.length <= 0)
			return 0;

		String ql = "DELETE FROM Biddinger T WHERE T.id in (:IDS)";

		List<String> params = new ArrayList<String>();

		String IDS = "";

		for (String id : ids) {
			if (StringUtils.isBlank(id))
				continue;

			params.add(id);

			if (!StringUtils.isBlank(IDS))
				IDS += ",";

			IDS += "?";
		}

		if (params.isEmpty())
			return 0;

		ql = ql.replace(":IDS", IDS);

		return this.deleteByQL(ql, params.toArray());
	}

}
